<script type="text/javascript">
  function timedRefresh(timeoutPeriod) {
    setTimeout("location.reload(true);",timeoutPeriod);
  }
</script>
<%
  contracts = Contract.find_by_sql "SELECT DISTINCT(NAME) as name,c_type,end_year FROM contracts WHERE game_id=#{@game.id} AND end_year>=#{@game.current_year} ORDER BY name,c_type"
%>
<%
  f1 = 25.0
  if @game.current_year==1
    f1 = @game_setting.f01_price
  else
    f1 = @game_setting.average_price * (1 + @game_setting.level1_debt_rate)
  end
  f2 = f1 * (1 + @game_setting.level1_debt_rate)
  f3 = f2 * (1 + @game_setting.level1_debt_rate)
  f1 = f1.round(2)
  f2 = f2.round(2)
  f3 = f3.round(2)

  last_f1 = nil
  last_f2 = nil
  last_f3 = nil
  last_f1 = @game_setting.last_price
  if last_f1
    last_f2 = last_f1 * (1 + @game_setting.level1_debt_rate)
    last_f3 = last_f2 * (1 + @game_setting.level1_debt_rate)
    last_f1 = last_f1.round(2)
    last_f2 = last_f2.round(2)
    last_f3 = last_f3.round(2)
  end
  
  avg_f1 = nil
  avg_f2 = nil
  avg_f3 = nil
  avg_f1 = @game_setting.average_price
  if avg_f1
    avg_f2 = avg_f1 * (1 + @game_setting.level1_debt_rate)
    avg_f3 = avg_f2 * (1 + @game_setting.level1_debt_rate)
    avg_f1 = avg_f1.round(2)
    avg_f2 = avg_f2.round(2)
    avg_f3 = avg_f3.round(2)
  end
%>
<center>
  <h2>สัญญาซื้อขายล่วงหน้า</h2>
  <table width="95%" style="border: 3px solid black; font-size: 24px;">
    <tr align="center" style="border: 3px solid black;" bgcolor="#AEEEEE">
      <th>สัญญา</th>
      <th>ประเภท</th>
      <th>ปีที่สิ้นอายุ<br/>สัญญา</th>
      <th>ราคาล่าสุด</th>
      <th>ราคาที่ใช้<br/>ชำระราคา</th>
      <!--th>เปลี่ยนแปลงจากราคา<br/>ที่ใช้ชำระล่าสุด</th-->
      <th>ปริมาณ</th>
      <th>มูลค่า</th>
      <th>สถานะ<br/>คงค้าง</th>
    </tr>
    <%
      all_total_trade = 0
      all_open = 0
      all_close = 0
    %>
    <%contracts.each_with_index do |c,i|%>
    <tr align="right" <%if i%2==1%>bgcolor="#dddddd"<%end%>>
      <td align="center"><%=c.name%></td>
      <td align="center"><%=c.c_type%></td>
      <td align="center"><%=c.end_year%></td>
      <td>
        <%if c.end_year-@game.current_year==0%>
          <%=number_with_precision(last_f1, :precision=>2)%>
        <%elsif c.end_year-@game.current_year==1%>
          <%=number_with_precision(last_f2, :precision=>2)%>
        <%else%>
          <%=number_with_precision(last_f3, :precision=>2)%>
        <%end%>
      </td>
      <td>
        <%if c.end_year-@game.current_year==0%>
          <%=number_with_precision(avg_f1, :precision=>2)%>
        <%elsif c.end_year-@game.current_year==1%>
          <%=number_with_precision(avg_f2, :precision=>2)%>
        <%else%>
          <%=number_with_precision(avg_f3, :precision=>2)%>
        <%end%>
      </td>
      <!--td></td-->
      <td><%=c_close = Contract.count(:conditions=>["game_id=? AND name=? AND c_type=? AND open_flag=? AND end_year>=?", @game.id, c.name, c.c_type, "N", @game.current_year])%></td>
      <%
        total_trade = 0
        trades = Contract.find(:all, :conditions=>["game_id=? AND name=? AND c_type=? AND open_flag=? AND end_year>=?", @game.id, c.name, c.c_type, "N", c.end_year])
        trades.each do |t|
          if t.settlement_price
            total_trade = (total_trade + (t.settlement_price * t.total_unit)).to_i
          end
        end
        all_total_trade = all_total_trade + total_trade
      %>
      <td><%=number_with_delimiter(total_trade)%></td>
      <td><%=c_open = Contract.count(:conditions=>["game_id=? AND name=? AND c_type=? AND open_flag=? AND end_year>=?", @game.id, c.name, c.c_type, "Y", @game.current_year])%></td>
      <%
        all_close = all_close + c_close
        all_open = all_open + c_open
      %>
    </tr>
    <%end%>
    <tr bgcolor="#AEEEEE" align="right">
      <td colspan="5">รวม </td>
      <td><%=all_close%></td>
      <td><%=number_with_delimiter(all_total_trade)%></td>
      <td><%=all_open%></td>
    </tr>
  </table>
</center>
<script type="text/javascript">
  timedRefresh(5000);
</script>